package action.report;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import exception.Exceptions;

import service.report.ReportContractService;
import util.Tool;

public class ReportContractAction {
	private ReportContractService reportContract;
	private String fromdate="";
	private String todate="";
	private List<Map<Object,Object>> contractlist;
	private String fileName;
	private Map<String,Object> sum=new HashMap<String,Object>();
	
	public String execute(){
		String abc="";
		if(!fromdate.equals("")||!todate.equals("")){
			abc=fromdate+"' and '"+todate;
		}
		
		contractlist=reportContract.getReportContractList(abc);
		
		BigDecimal sumYingshoulixi=new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal sumShishoulixi=new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal sumJiekuan=new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);

		BigDecimal ttt=new BigDecimal(0);
		for(Map<Object,Object> tmp: contractlist){
			if(tmp.get("interests")!=null){
				ttt=new BigDecimal(tmp.get("interests").toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
				sumYingshoulixi = sumYingshoulixi.add(ttt);
			}
			if(tmp.get("paidin")!=null){
				ttt=new BigDecimal(tmp.get("paidin").toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
				sumShishoulixi=sumShishoulixi.add(ttt);
			}
			if(tmp.get("grantmoney")!=null){
				ttt=new BigDecimal(tmp.get("grantmoney").toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
				sumJiekuan=sumJiekuan.add(ttt);
			}
		}
		sum.put("sumYingshoulixi", sumYingshoulixi);
		sum.put("sumShishoulixi", sumShishoulixi);
		sum.put("sumWeishoulixi", sumYingshoulixi.subtract(sumShishoulixi));
		sum.put("sumJiekuan", sumJiekuan);
		return "success";
	}
	
	public String reportExport(){
		return "report";
	}

	@SuppressWarnings("deprecation")
	public InputStream getInputStream() throws Exceptions {
		String abc="";
		if(!fromdate.equals("")||!todate.equals("")){
			abc=fromdate+"' and '"+todate;
		}
		List<Map<Object,Object>> list=reportContract.getReportContractList(abc);
		
		//下载文件名
        fileName=Tool.toChinese("月贷款利息台账.xls");
		String[] titles={"借款人名称","次序","借款日期","到期日期","借款金额","余额","期限","利率","还款日","上个计息日","计息日","天数","应计利息","实收利息","未收利息","担保方式"};
		String[] contents={"customname","grantnum","grantdate","refunddate","grantmoney","balance","dd","performrates","repaydate","lastvaluedate","valuedate","days","interests","paidin","nopaidin"};
		
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFRow row = sheet.createRow((int)0);
        
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        
        HSSFCell cell = null;
        
        //title
        int titleSize=titles.length;
        for(int i=0;i<titleSize;i++){
        	cell=row.createCell((short)i);
        	cell.setCellValue(titles[i]);
        	cell.setCellStyle(style);
        }
    
        //content
        int listSize=list.size();
        int contentsSize=contents.length;
        for(int j=0;j<listSize;j++){
            row = sheet.createRow(j+1);
            Map<Object,Object> tmp=list.get(j);
            for(int i=0;i<contentsSize;i++){
            	cell=row.createCell((short)i);
            	Object aaa=tmp.get(contents[i]);
            	if(aaa!=null){
            		cell.setCellValue(aaa.toString());
            	}
            }
        }

        //输出下载
		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
		try {
			wb.write(baos);
		} catch (IOException e) {
			e.printStackTrace();
		}
        return new ByteArrayInputStream(baos.toByteArray());
	}
	
	public ReportContractService getReportContract() {
		return reportContract;
	}
	public void setReportContract(ReportContractService reportContract) {
		this.reportContract = reportContract;
	}
	public String getFromdate() {
		return fromdate;
	}
	public void setFromdate(String fromdate) {
		this.fromdate = fromdate;
	}
	public String getTodate() {
		return todate;
	}
	public void setTodate(String todate) {
		this.todate = todate;
	}
	public Map<String,Object> getSum() {
		return sum;
	}
	public void setSum(Map<String,Object> sum) {
		this.sum = sum;
	}

	public List<Map<Object,Object>> getContractlist() {
		return contractlist;
	}

	public void setContractlist(List<Map<Object,Object>> contractlist) {
		this.contractlist = contractlist;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}
